package com.cvte.vdt.uartservice;

import android.app.Instrumentation;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.view.KeyEvent;

import com.cvte.vdt.uartservice.aidl.IVDTUartBluetoothAidl;
import com.cvte.vdt.uartservice.aidl.IVDTUartCallback;
import com.cvte.vdt.uartservice.dao.BluetoothDevDao;
import com.cvte.vdt.uartservice.dao.BluetoothDevDaoSQliteImp;
import com.cvte.vdt.uartservice.serial.SerialPortUtil;
import com.cvte.vdt.uartservice.util.BtSharePreferencesUtil;
import com.cvte.vdt.uartservice.util.BtTasksUtil;
import com.cvte.vdt.uartservice.util.CallbackUtil;
import com.cvte.vdt.uartservice.util.DataUtil;
import com.cvte.vdt.uartservice.util.LogUtil;
import com.cvte.vdt.uartservice.util.PropUtil;
import com.cvte.vdtlibrary.bluetooth.EntityBluetoothDevice;
import com.cvte.vdtlibrary.bluetooth.EnumBTRecModeStatus;
import com.cvte.vdtlibrary.bluetooth.EnumBTSendModeStatus;
import com.cvte.vdtlibrary.bluetooth.EnumBluetoothKey;
import com.cvte.vdtlibrary.bluetooth.EnumBluetoothMode;
import com.cvte.vdtlibrary.bluetooth.EnumBtDeviceStatus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuguitong on 2018/11/19.
 */
public class VDTService extends Service implements SerialPortUtil.OnDataReceiveListener,BtTasksUtil.OnTaskCallback{
    private static final String TAG = "VDTService";
    private Context context;
    private EnumBluetoothMode curMode;
    private EnumBTSendModeStatus curSendStatus;
    private EnumBTRecModeStatus curRecStatus;
    private String curSendModeVer;
    private String curRecModeVer;
    private String curMusicName;
    private EntityBluetoothDevice curDevice;
    private List<EntityBluetoothDevice> devList;
    private BluetoothDevDao dao;
    private String curMacConnected;
    private boolean isPowerOn;
    @Override
    public void onCreate() {
        super.onCreate();
        LogUtil.d(TAG,"onCreate");
        context = this;
        resetStatusPara();
        devList = new ArrayList<>();
        dao = new BluetoothDevDaoSQliteImp(context);
        dao.cleanNotPairBtDev();
        dao.initBtDevStatus();
        CallbackUtil.start();
        PropUtil.PropInitUart1();
        DataUtil.initSerialDataUtil();
        BtTasksUtil.initTasks(this,this);
    }

    @Override
    public void onDestroy() {
        LogUtil.d(TAG,"onDestroy");
        CallbackUtil.kill();
        BtTasksUtil.deinitTasks();
        DataUtil.deinitSerialDataUtil();
        resetStatusPara();
        super.onDestroy();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LogUtil.d(TAG,"onBind");
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        LogUtil.d(TAG,"onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDataReceive(byte[] buffer, int size) {
        int datasize = DataUtil.decodeReceBuff(buffer,size);
        for(int i = 0;i < datasize; i++)
        {
            DataUtil.ReceData data = DataUtil.getCurrentReceData();
            if(data != null && mUartRecHandler != null) {
                Message msg = mUartRecHandler.obtainMessage(data.getCommandId());
                Bundle mBundle = new Bundle();
                mBundle.putByteArray(DataUtil.DATA,data.getReceData());
                mBundle.putByte(DataUtil.COMMAND,data.getCommandId());
                msg.setData(mBundle);
                msg.sendToTarget();
            }
        }
    }

    @Override
    public void OnTimeOutCallback(int cmd) {
        if(cmd == DataUtil.COM_SEND_CANEL_CONNECT
                || cmd == DataUtil.COM_SEND_CONNECT_DEVICE){
            dao.initBtDevStatus();
            CallbackUtil.callbackonBluetoothSendDataChange(dao.getAllBtDevList());
        }
    }

    private final IVDTUartBluetoothAidl.Stub mBinder = new IVDTUartBluetoothAidl.Stub(){
        @Override
        public boolean setBluetoothMode(EnumBluetoothMode bluetoothMode) throws RemoteException {
            LogUtil.d(TAG,"setBluetoothMode");
            if(curMode == bluetoothMode){
                return false;
            }else {
                BtTasksUtil.cleanTasks();
                if (bluetoothMode == EnumBluetoothMode.SEND_MODE) {
                    curMode = EnumBluetoothMode.TRANSFER_SEND;
                    dao.cleanNotPairBtDev();
                } else {
                    curMode = EnumBluetoothMode.TRANSFER_REC;
                }
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COMMAND_RESET),4000);
                return true;
            }
        }

        @Override
        public EnumBluetoothMode getBluetoothMode() throws RemoteException {
            return curMode;
        }

        @Override
        public boolean getBTSendModeOnOff() throws RemoteException {
            return BtSharePreferencesUtil.getBtSendModOnOff(context);
        }

        @Override
        public boolean setBTSendModeOnOff(boolean b) throws RemoteException {
            if( BtSharePreferencesUtil.getBtSendModOnOff(context) != b) {
                byte[] sendBytes = b ? new byte[]{(byte) 0x01} :new byte[]{(byte) 0x00};
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COMMAND_POWER_ONOFF, sendBytes));
            }
            return BtSharePreferencesUtil.saveBtSendOnOff(context,b);
        }

        @Override
        public boolean powerOnBluetooth() throws RemoteException {
            if(!isPowerOn) {
                byte[] sendBytes = new byte[]{(byte) 0x01};
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COMMAND_POWER_ONOFF, sendBytes), BtTasksUtil.PRIORITY.HIGH);
                return true;
            }
            return false;
        }

        @Override
        public boolean powerOffBluetooth() throws RemoteException {
            if(isPowerOn) {
                byte[] sendBytes = new byte[]{(byte) 0x00};
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COMMAND_POWER_ONOFF, sendBytes), BtTasksUtil.PRIORITY.HIGH);
                return true;
            }
            return false;
        }

        @Override
        public boolean isBluetoothPowerOn() throws RemoteException {
            return isPowerOn;
        }

        @Override
        public EntityBluetoothDevice getConnectBTDevice() throws RemoteException {
            return curDevice;
        }

        @Override
        public List<EntityBluetoothDevice> getPairBTList() throws RemoteException {
            return dao.getPairBtDevList();
        }

        @Override
        public List<EntityBluetoothDevice> getNotPairBTList() throws RemoteException {
            return dao.getNotPairBtDevList();
        }

        @Override
        public void cleanNotPairBTList() throws RemoteException {
            dao.cleanNotPairBtDev();
        }

        @Override
        public String getBTVersion() throws RemoteException {
            if(curMode == EnumBluetoothMode.SEND_MODE){
                if(curSendModeVer.equals(DataUtil.DEF_SEND_VER)){
                    BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_SEND_GET_VERSION), BtTasksUtil.PRIORITY.LOW);
                }
                return curSendModeVer;
            }else{
                if(curRecModeVer.equals(DataUtil.DEF_REC_VER)){
                    BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_REC_GET_VERSION), BtTasksUtil.PRIORITY.LOW);
                }
                return curRecModeVer;
            }
        }

        @Override
        public int getBTVolume() throws RemoteException {
            return 0;
        }

        @Override
        public String getBTRecMusicName() throws RemoteException {
            return curMusicName;
        }

        @Override
        public boolean deleteBTRecPairDevice(EntityBluetoothDevice entityBluetoothDevice) throws RemoteException {
            if(curMode == EnumBluetoothMode.REC_MODE) {
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_REC_DELETE_DEVICE, DataUtil.strToByteArray(entityBluetoothDevice.getMAC())));
                return true;
            }
            return false;
        }

        @Override
        public boolean connectBTRecDevice(EntityBluetoothDevice entityBluetoothDevice) throws RemoteException {
            return false;
        }

        @Override
        public boolean disconnectBTRecDevice(EntityBluetoothDevice entityBluetoothDevice) throws RemoteException {
            if(curMode == EnumBluetoothMode.REC_MODE) {
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_REC_CANEL_CONNECT, DataUtil.strToByteArray(entityBluetoothDevice.getMAC())));
                return true;
            }
            return false;
        }

        @Override
        public void sendBTRecModeKey(EnumBluetoothKey enumBluetoothKey) throws RemoteException {
            if(curMode == EnumBluetoothMode.REC_MODE){
                byte[] key = new byte[1];
                switch (enumBluetoothKey){
                    case KEY_PLAY_PAUSE:
                        key[0] = 0x00;
                        break;
                    case KEY_PREV:
                        key[0] = 0x01;
                        break;
                    case KEY_NEXT:
                        key[0] = 0x02;
                        break;
                    default:
                        return;
                }
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_REC_MUSIC_ACTION,key),10);
            }
        }

        @Override
        public EnumBTRecModeStatus getBTRecStatus() throws RemoteException {
            return curRecStatus;
        }

        @Override
        public boolean startSearchBTSendMode() throws RemoteException {
            if(curSendStatus.ordinal() > EnumBTSendModeStatus.STATUS_POWEROFF.ordinal()){
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_SEND_SEARCH),20000);
                curSendStatus = EnumBTSendModeStatus.STATUS_SEARCH_START;
                CallbackUtil.callbackOnBluetoothSendModeStatusChange(curSendStatus,null,null);
                return true;
            }
            return false;
        }

        @Override
        public boolean stopSearchBTSendMode() throws RemoteException {
            if(curSendStatus == EnumBTSendModeStatus.STATUS_SEARCH_START) {
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_SEND_CANEL_SEARCH));
                return true;
            }
            return false;
        }

        @Override
        public EnumBTSendModeStatus getBTSendStatus() throws RemoteException {
            return curSendStatus;
        }

        @Override
        public boolean deleteBTSendPairDevice(EntityBluetoothDevice entityBluetoothDevice) throws RemoteException {
            if(curMode == EnumBluetoothMode.SEND_MODE){
                dao.updateBtDevPairStatus(entityBluetoothDevice.getMAC(),EnumBtDeviceStatus.DEV_NOT_PAIR);
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_SEND_DELETE_DEVICE,DataUtil.strToByteArray(entityBluetoothDevice.getMAC())));
                CallbackUtil.callbackonBluetoothSendDataChange(dao.getAllBtDevList());
            }
            return true;
        }

        @Override
        public boolean connectBTSendDevice(EntityBluetoothDevice entityBluetoothDevice) throws RemoteException {
            if(curMode == EnumBluetoothMode.SEND_MODE) {
                if(!curMacConnected.equals(DataUtil.DEFAULT_MAC)){
                    disconnectBTSendDevice(new EntityBluetoothDevice(curMacConnected,DataUtil.DEFAULT_NAME));
                }
                dao.updateBtDevPairStatus(entityBluetoothDevice.getMAC(), EnumBtDeviceStatus.DEV_CONNECTING);
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_SEND_CONNECT_DEVICE, DataUtil.strToByteArray(entityBluetoothDevice.getMAC())),4000);
                CallbackUtil.callbackonBluetoothSendDataChange(dao.getAllBtDevList());
            }
            return true;
        }

        @Override
        public boolean disconnectBTSendDevice(EntityBluetoothDevice entityBluetoothDevice) throws RemoteException {
            if(curMode == EnumBluetoothMode.SEND_MODE) {
                dao.updateBtDevPairStatus(entityBluetoothDevice.getMAC(), EnumBtDeviceStatus.DEV_DIS_CONNECTING);
                BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_SEND_CANEL_CONNECT, DataUtil.strToByteArray(entityBluetoothDevice.getMAC())),4000);
                CallbackUtil.callbackonBluetoothSendDataChange(dao.getAllBtDevList());
                return true;
            }
            return false;
        }

        @Override
        public void registerVDTUartCallback(IVDTUartCallback ivdtUartCallback) throws RemoteException {
            LogUtil.d(TAG,"registerVDTUartCallback");
            if(ivdtUartCallback != null){
                LogUtil.d(TAG,"registerVDTUartCallback  ivdtUartCallback != null");
                CallbackUtil.register(ivdtUartCallback);
            }
        }

        @Override
        public void unregisterVDTUartCallback(IVDTUartCallback ivdtUartCallback) throws RemoteException {
            if(ivdtUartCallback != null){
                CallbackUtil.unregister(ivdtUartCallback);
            }
        }
    };
    private Handler mUartRecHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Bundle receBundle = msg.getData();
            byte tmpCommand = receBundle.getByte(DataUtil.COMMAND);
            byte[] tmpRecData = receBundle.getByteArray(DataUtil.DATA);
            byte[] tmpCommandArry = {tmpCommand};
            byte[] sendBytes;
            LogUtil.d(TAG,"command = " + DataUtil.printHexBinary(tmpCommandArry) + " data = " + DataUtil.printHexBinary(tmpRecData));
            switch (tmpCommand){
                //common
                case DataUtil.RESULT_POWER_REMIND:
                    BtTasksUtil.finishTaskByID(DataUtil.COMMAND_RESET);
                    if(curMode != EnumBluetoothMode.TRANSFER_SEND && curMode != EnumBluetoothMode.TRANSFER_REC){
                        resetStatusPara();
                        dao.initBtDevStatus();
                    }
                    if(curMode == EnumBluetoothMode.TRANSFER_SEND){
                        BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COMMAND_SELECT_SEND_MODE));
                    }else if(curMode == EnumBluetoothMode.TRANSFER_REC){
                        BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COMMAND_SELECT_REC_MODE));
                    }
                    break;
                case DataUtil.RESULT_SEND_MODE_INIT:
                    BtTasksUtil.finishTaskByID(DataUtil.COMMAND_SELECT_SEND_MODE);
                    curMode = EnumBluetoothMode.SEND_MODE;
                    if( BtSharePreferencesUtil.getBtSendModOnOff(context)) {
                        sendBytes = new byte[] {(byte)0x01};
                        BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COMMAND_POWER_ONOFF,sendBytes));
                    }
                    BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_SEND_GET_VERSION), BtTasksUtil.PRIORITY.LOW);
                    break;
                case DataUtil.RESULT_REC_MODE_INIT:
                    BtTasksUtil.finishTaskByID(DataUtil.COMMAND_SELECT_REC_MODE);
                    curMode = EnumBluetoothMode.REC_MODE;
                    sendBytes =new byte[] {(byte)0x01};
                    BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COMMAND_POWER_ONOFF, sendBytes));
                    BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_REC_GET_VERSION), BtTasksUtil.PRIORITY.LOW);
                    break;
                case DataUtil.RESULT_POWER_STATUS:
                    if(tmpRecData != null) {
                        BtTasksUtil.finishTaskByID(DataUtil.COMMAND_POWER_ONOFF);
                        if(curMode == EnumBluetoothMode.SEND_MODE) {
                            curSendStatus = ((tmpRecData[0] == (byte) 0x01) ? EnumBTSendModeStatus.STATUS_POWERON : EnumBTSendModeStatus.STATUS_POWEROFF);
                            CallbackUtil.callbackOnBluetoothSendModeStatusChange(curSendStatus,null,null);
                        }else if(curMode == EnumBluetoothMode.REC_MODE){
                            curRecStatus = ((tmpRecData[0] == (byte)0x01) ? EnumBTRecModeStatus.STATUS_POWERON : EnumBTRecModeStatus.STATUS_POWEROFF);
                            CallbackUtil.callbackOnBluetoothRecModeStatusChange(curRecStatus,null,null);
                        }
                        isPowerOn = (tmpRecData[0] == (byte) 0x01);
                        if((tmpRecData[0] == (byte) 0x00) && curMode != EnumBluetoothMode.TRANSFER_REC && curMode != EnumBluetoothMode.TRANSFER_SEND){
                            resetStatusPara();
                        }
                    }
                    break;
                //send
                case DataUtil.RES_SEND_A2DP_STATUS:
                    if(tmpRecData != null) {
                        switch (tmpRecData[0]){
                            case DataUtil.DATA_SEND_A2DP_STATUS_READY:
                                curSendStatus = EnumBTSendModeStatus.STATUS_A2DP_READY;
                                dao.initBtDevStatus();
                                break;
                            case DataUtil.DATA_SEND_A2DP_STATUS_CONNECTED:
                                curSendStatus = EnumBTSendModeStatus.STATUS_A2DP_CONNECTED;
                                break;
                            case DataUtil.DATA_SEND_A2DP_STATUS_STREAMING:
                                curSendStatus = EnumBTSendModeStatus.STATUS_A2DP_STREAMING;
                                break;
                            case DataUtil.DATA_SEND_A2DP_STATUS_PAUSED:
                                curSendStatus = EnumBTSendModeStatus.STATUS_A2DP_PAUSED;
                                break;
                        }
                        CallbackUtil.callbackOnBluetoothSendModeStatusChange(curSendStatus,null,null);
                    }
                    break;
                case DataUtil.RES_SEND_SEARCH_END:
                    BtTasksUtil.finishTaskByID(DataUtil.COM_SEND_SEARCH);
                    curSendStatus = EnumBTSendModeStatus.STATUS_SEARCH_END;
                    List<EntityBluetoothDevice> devList = dao.getAllBtDevList();
                    for(EntityBluetoothDevice dev:devList){
                        BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_SEND_GET_NAME,DataUtil.strToByteArray(dev.getMAC())),BtTasksUtil.PRIORITY.LOW,100);
                        LogUtil.d(TAG,"RES_SEND_SEARCH_END find  " + dev.getMAC());
                    }
                    CallbackUtil.callbackOnBluetoothSendModeStatusChange(curSendStatus,null,devList);
                    break;
                case DataUtil.RES_SEND_SEARCH_FIND:
                    if(tmpRecData != null) {
                        dao.addBtDev(DataUtil.byteArrayToStr(tmpRecData));
                        LogUtil.d(TAG,"RES_SEND_SEARCH_FIND " + DataUtil.byteArrayToStr(tmpRecData));
                        CallbackUtil.callbackOnBluetoothSendModeStatusChange(EnumBTSendModeStatus.STATUS_SEARCH_FIND,null,dao.getAllBtDevList());
                    }
                    break;
                case DataUtil.RES_SEND_GET_NAME:
                    if(tmpRecData != null){
                        String mac = BtTasksUtil.getFIFOMac();
                        if((mac != DataUtil.DEFAULT_MAC) && !(DataUtil.byteArrayToStr(tmpRecData).trim().equals(DataUtil.DEFAULT_NAME))) {
                            dao.updateBtDevName(mac,DataUtil.byteArrayToStr(tmpRecData));
                        }
                        LogUtil.d(TAG,"RES_SEND_GET_NAME " + DataUtil.byteArrayToStr(tmpRecData));
                        //BtTasksUtil.finishTaskByID(DataUtil.COM_SEND_GET_NAME);
                        CallbackUtil.callbackOnBluetoothSendModeStatusChange(EnumBTSendModeStatus.STATUS_GET_NAME,null,dao.getAllBtDevList());
                    }
                    break;
                case DataUtil.RES_SEND_GET_VERSION:
                    if(tmpRecData != null){
                        BtTasksUtil.finishTaskByID(DataUtil.COM_SEND_GET_VERSION);
                        curSendModeVer = DataUtil.byteArrayToStr(tmpRecData);
                        CallbackUtil.callbackOnGetBtVersion(curMode,curSendModeVer);
                        LogUtil.d(TAG,"RES_SEND_GET_VERSION " + curSendModeVer);
                    }
                    break;
                case DataUtil.RES_SEND_POWER_STATUS:
                    BtTasksUtil.finishTaskByID(DataUtil.COMMAND_POWER_ONOFF);
                    if(tmpRecData != null) {
                        if (curMode == EnumBluetoothMode.SEND_MODE) {
                            curSendStatus = ((tmpRecData[0] == (byte) 0x01) ? EnumBTSendModeStatus.STATUS_POWERON : EnumBTSendModeStatus.STATUS_POWEROFF);
                            CallbackUtil.callbackOnBluetoothSendModeStatusChange(curSendStatus, null, null);
                        }
                    }
                    break;
                case DataUtil.RES_SEND_A2DP_CON_SUC:
                    BtTasksUtil.finishTaskByID(DataUtil.COM_SEND_CONNECT_DEVICE);
                    curMacConnected = DataUtil.byteArrayToStr(tmpRecData);
                    String mac = DataUtil.byteArrayToStr(tmpRecData);
                    LogUtil.d(TAG,"cur connected MAC = "  +  mac);
                    if(!dao.isContainBtDev(curMacConnected)){
                        dao.addBtDev(curMacConnected);
                    }
                    dao.updateBtDevPairStatus(mac, EnumBtDeviceStatus.DEV_CONNECTED);
                    BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_SEND_GET_NAME,tmpRecData), BtTasksUtil.PRIORITY.LOW,100);
                    CallbackUtil.callbackOnBluetoothSendModeStatusChange(EnumBTSendModeStatus.STATUS_A2DP_CON_SUC,null,dao.getAllBtDevList());
                    break;
                case DataUtil.RES_SEND_A2DP_CON_CANEL:
                    BtTasksUtil.finishTaskByID(DataUtil.COM_SEND_CANEL_CONNECT);
                    if(dao.getBtDevByKey(curMacConnected).getStatus() != EnumBtDeviceStatus.DEV_NOT_PAIR) {
                        dao.updateBtDevPairStatus(curMacConnected, EnumBtDeviceStatus.DEV_PAIR);
                    }
                    CallbackUtil.callbackOnBluetoothSendModeStatusChange(EnumBTSendModeStatus.STATUS_A2DP_DISCON,null,dao.getAllBtDevList());
                    curMacConnected = DataUtil.DEFAULT_MAC;
                    break;
                case DataUtil.RES_SEND_A2DP_CON_FAIL:
                    curMacConnected = DataUtil.DEFAULT_MAC;
                    BtTasksUtil.finishTaskByID(DataUtil.COM_SEND_CONNECT_DEVICE);
                    dao.updateBtDevPairStatus(DataUtil.byteArrayToStr(tmpRecData), EnumBtDeviceStatus.DEV_PAIR);
                    CallbackUtil.callbackOnBluetoothSendModeStatusChange(EnumBTSendModeStatus.STATUS_A2DP_CON_FAIL,null,dao.getAllBtDevList());
                    break;
                case DataUtil.RES_SEND_GET_ACTION:
                    if(tmpRecData != null){
                        LogUtil.d(TAG,"RES_SEND_GET_ACTION = "  +  tmpRecData[0]);
                        switch (tmpRecData[0]){
                            case DataUtil.MUSIC_PLAY_SENDMODE:
                                simulateKeystroke(KeyEvent.KEYCODE_MEDIA_PLAY);
                                break;
                            case DataUtil.MUSIC_PAUSE_SENDMODE:
                                simulateKeystroke(KeyEvent.KEYCODE_MEDIA_PAUSE);
                                break;
                            case DataUtil.MUSIC_STOP_SENDMODE:
                                simulateKeystroke(KeyEvent.KEYCODE_MEDIA_STOP);
                                break;
                            case DataUtil.MUSIC_NEXT_SENDMODE:
                                simulateKeystroke(KeyEvent.KEYCODE_MEDIA_NEXT);
                                break;
                            case DataUtil.MUSIC_PRE_SENDMODE:
                                simulateKeystroke(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
                                break;
                        }
                    }
                    break;
                case DataUtil.RES_SEND_PAIR_SUC:
                    if(tmpRecData != null) {
                        curMacConnected = DataUtil.byteArrayToStr(tmpRecData);
                        dao.updateBtDevPairStatus(curMacConnected,EnumBtDeviceStatus.DEV_PAIR);
                        LogUtil.d(TAG,"RES_SEND_PAIR_SUC = "  + curMacConnected);
                    }
                    break;
                //rec
                case DataUtil.RES_REC_GET_VERSION:
                    if(tmpRecData != null){
                        BtTasksUtil.finishTaskByID(DataUtil.COM_REC_GET_VERSION);
                        curRecModeVer = DataUtil.byteArrayToStr(tmpRecData);
                        CallbackUtil.callbackOnGetBtVersion(curMode,curRecModeVer);
                        LogUtil.d(TAG,"RES_REC_GET_VERSION " + curRecModeVer);
                    }
                    break;
                case DataUtil.RES_REC_CUR_MUSIC_NAME:
                    if(tmpRecData != null){
                        String tmpMusicName = DataUtil.byteArrayToStr(tmpRecData);
                        LogUtil.d(TAG,"RES_REC_CUR_MUSIC_NAME " + tmpMusicName);
                        if(!tmpMusicName.equals(curMusicName)) {
                            curMusicName = tmpMusicName;
                            CallbackUtil.callbackOnBluetoothRecMusicNameChange(curMusicName);
                        }
                    }
                    break;
                case DataUtil.RES_REC_A2DP_STATUS:
                    if(tmpRecData != null) {
                        switch (tmpRecData[0]){
                            case DataUtil.DATA_REC_A2DP_STATUS_READY:
                                curRecStatus = EnumBTRecModeStatus.STATUS_A2DP_READY;
                                break;
                            case DataUtil.DATA_REC_A2DP_STATUS_CONNECTED:
                                curRecStatus = EnumBTRecModeStatus.STATUS_A2DP_CONNECTED;
                                break;
                            case DataUtil.DATA_REC_A2DP_STATUS_STREAMING:
                                curRecStatus = EnumBTRecModeStatus.STATUS_A2DP_STREAMING;
                                break;
                        }
                        CallbackUtil.callbackOnBluetoothRecModeStatusChange(curRecStatus,null,null);
                    }
                    break;
                case DataUtil.RES_REC_A2DP_CONNECT_SUC:
                    BtTasksUtil.addTask(DataUtil.getComBuff(DataUtil.COM_REC_GET_NAME,tmpRecData), BtTasksUtil.PRIORITY.LOW);
                    break;
                case DataUtil.RES_REC_A2DP_CANEL_CONNECT:
                    LogUtil.d(TAG,"RES_REC_A2DP_CANEL_CONNECT");
                    curMusicName = DataUtil.DEFAULT_NAME;
                    CallbackUtil.callbackOnBluetoothRecConnectedNameChange(DataUtil.DEFAULT_NAME);
                    CallbackUtil.callbackOnBluetoothRecMusicNameChange(DataUtil.DEF_MUSIC_NAME);
                    break;
                case DataUtil.RES_REC_GET_NAME:
                    BtTasksUtil.finishTaskByID(DataUtil.COM_REC_GET_NAME);
                    if(tmpRecData != null) {
                        CallbackUtil.callbackOnBluetoothRecConnectedNameChange(DataUtil.byteArrayToStr(tmpRecData));
                    }
                    break;
                default:
                    break;
            }
        }
    };
    private void resetStatusPara(){
        curSendStatus = EnumBTSendModeStatus.STATUS_UNINIT;
        curRecStatus = EnumBTRecModeStatus.STATUS_UNINIT;
        curMacConnected = DataUtil.DEFAULT_MAC;
        curSendModeVer = DataUtil.DEF_SEND_VER;
        curRecModeVer = DataUtil.DEF_REC_VER;
        curMode = EnumBluetoothMode.TRANSFER_SEND;
        curMusicName = DataUtil.DEF_MUSIC_NAME;
        isPowerOn = false;
        curDevice = null;
    }
    public static void simulateKeystroke(final int KeyCode) {


        new Thread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                try {

                    Instrumentation inst=new Instrumentation();
                    inst.sendKeyDownUpSync(KeyCode);
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }).start();
    }
}
